package org.blueoxygen.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import org.blueoxygen.cimande.commons.CimandeModuleAction;
import org.blueoxygen.entity.Absen;
import org.blueoxygen.service.AbsenService;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class AbsenController extends CimandeModuleAction {
	@Autowired
	private AbsenService absenService;
	
	private Absen absen = new Absen();
	private int limit = 0;
	private int page = 0;

	SimpleDateFormat df = new SimpleDateFormat("dd-MM-yy");

	public AbsenController() {
		model.put("absen", absen);
	}

	// POST /module/person/create
	// POST /module/person/create/{id}
	@Validations(requiredStrings = {
	@RequiredStringValidator(fieldName = "person.namad", message = "nama depan tidak boleh kosong", trim = true),
	@RequiredStringValidator(fieldName = "person.namab", message = "nama belakang tidak boleh kosong", trim = true) })

/*	public String personPost() {

		try {
			absen.setBirthdate(df.parse(model.get("birthdate").toString()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		personService.savePerson(person);

		return SUCCESS;
	} */

	// GET /module/person/create
	// GET /module/person/edit/{id}
	public String personGet() {
		model.put("absen", absenService.getAbsenById(absen.getId() + ""));

		// model.put("itemCategorys", itemCategoryService.getAllItemCategory());
		return INPUT;
	}

	// DELETE /module/person/edit/{id}
	public String absenhapus() {
		absenService.hapusAbsen(absenService.getAbsenById(absen.getId()));

		return SUCCESS;
	}

	// POST/GET /module/person/filter
	public String filter() {

		try {
			limit = model.get("rows") == null ? 0 : new Integer(model.get(
					"rows").toString());
			page = model.get("page") == null ? 0 : new Integer(model
					.get("page").toString());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		if (limit == 0 && page == 0) {
			limit = 10;
			page = 1;

		}

		model.put("rows", limit);
		model.put("page", page);

		int count = (int) absenService.getPersonCount("", "");
		int total = count / limit;
		if (total % limit > 0)
			total++;

		System.out.println();
		model.put("persons", absenService.getAbsenList("", "", limit, page));

		if (total == 0)
			total++;

		model.put("total", total);
		model.put("records", total);

		return SUCCESS;
	}

}
